﻿using BZ.Core.Frm;
using NDK.Motion.Card;
using NDK.Motion.Frm;
using HZH_Controls.Forms;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace NDK.Motion.MachineManager
{
    public partial class Dlg_AddCard : FrmWithOKCancel1
    {
        private static log4net.ILog traceLogger = log4net.LogManager.GetLogger("Dlg_CardConfig");

        public List<ICard> MotionCards { get; private set; }
        public List<ICard> IOCards { get; private set; }

        public Dlg_AddCard()
        {
            InitializeComponent();
            MotionCards = new List<ICard>();
            IOCards = new List<ICard>();

            //CardType下拉框列出所有枚举CARD_TYPE的值
            List<KeyValuePair<string, string>> lstCardType = new List<KeyValuePair<string, string>>();
            foreach (int value in Enum.GetValues(typeof(CARD_TYPE)))
            {
                string name = Enum.GetName(typeof(CARD_TYPE), value).ToString();
                lstCardType.Add(new KeyValuePair<string, string>(value.ToString(), name));
            }
            cb_CardType.Source = lstCardType;
            cb_CardType.Font = new System.Drawing.Font("微软雅黑", 9F, FontStyle.Regular, GraphicsUnit.Pixel, ((byte)(134)));
        }

        /// <summary>
        /// Does the enter.
        /// </summary>
        protected override void DoEnter()
        {
            if (string.IsNullOrWhiteSpace(cb_CardType.TextValue))
            {
                FrmTips.ShowTipsInfo(this, "[ CardType ]必须输入。");
                this.ActiveControl = cb_CardType.txtInput;
                return;
            }
            base.DoEnter();
        }

        private void cb_CardType_TextChangedEvent(object sender, EventArgs e)
        {
            btn_FindCard.Visible = true;
            btn_ManualAdd.Visible = true;
        }

        private void btn_FindCard_BtnClick(object sender, EventArgs e)
        {
            FindCard((CARD_TYPE)cb_CardType.SelectedIndex);
        }
        private void btn_ManualAdd_BtnClick(object sender, EventArgs e)
        {
            Dlg_ManualAddCard dlg_ManualAddCard = new Dlg_ManualAddCard();
            if (dlg_ManualAddCard.ShowDialog() == DialogResult.OK)
            {
                ManualAddCard((CARD_TYPE)cb_CardType.SelectedIndex,
                              dlg_ManualAddCard.CardID,
                              dlg_ManualAddCard.StartAxisID,
                              dlg_ManualAddCard.TotalAxisNum,
                              dlg_ManualAddCard.DINum,
                              dlg_ManualAddCard.DONum); ;
            }


        }

        /// <summary>
        /// 搜索选中的卡类型
        /// </summary>
        /// <param name="type"></param>
        private void FindCard(CARD_TYPE type)
        {
            ICard card = new Adlink();
            if (type == CARD_TYPE.ADLINK_2048)
            {
                card = new Adlink();
            }
            else if (type == CARD_TYPE.ADLINK_833X)
            {
                card = new Adlink833X();
            }
            else if (type == CARD_TYPE.ACS)
            {
                card = new ACSController();
            }
            else if (type == CARD_TYPE.M60)
            {
                card = new LCT_M60();
            }
            else if (type == CARD_TYPE.Beckoff)
            {
                card = new Beckhoff();
            }
            else if (type == CARD_TYPE.ADLINK_IO_CARD)
            {
                card = new AdlinkIOCard();
            }
            else if (type == CARD_TYPE.UWC348A_IO_CARD)
            {
                card = new UWC348AIOCard();
            }


            //Test start
            //AdlinkMotionCards.Clear();
            //for (int i = 0; i < 2; i++)//模拟找到两张卡，每个卡4个轴
            //{
            //    card = new Adlink();
            //    card.TotalAxis = 4;
            //    card.StartAxisID = i * card.TotalAxis;
            //    card.CardID = i;
            //    card.CardType = "PCI_20408C";
            //    card.InputCount = 24;
            //    card.OutputCount = 24;
            //    MotionCards.Add(card);
            //}
            //Test end
            List<ICard> cards = new List<ICard>();
            if (card is IMotionCard)
            {
                try
                {
                    card.CardInit(ref cards, 0);
                    MotionCards.AddRange(cards);
                }
                catch (Exception ex)
                {
                    traceLogger.Error(ex.StackTrace);
                }

                string content = $"Search for { MotionCards.Count}  Motion Card （{card.CardType}）" + Environment.NewLine;
                foreach (IMotionCard c in MotionCards)
                {
                    content += "Card ID:" + c.CardID + "  Includs " + c.TotalAxis + " Axes" + Environment.NewLine;
                }
                Dlg_Confirm comfirmDlg = new Dlg_Confirm("Confirm", content + "Click Confirm Add to Resources Tree");
                comfirmDlg.Size = new Size(400, 400);
                if (comfirmDlg.ShowDialog() == DialogResult.Cancel)
                {
                    MotionCards.Clear();
                }
            }
            else
            {
                card.CardInit(ref cards, 0);
               
                IOCards.AddRange(cards);

                string content = $"Search for {cards.Count} IO Card ({card.CardType})" + Environment.NewLine;
                Dlg_Confirm comfirmDlg = new Dlg_Confirm("Confirm", content + "Click Confirm Add to Resources Tree");
                comfirmDlg.Size = new Size(400, 400);
                if (comfirmDlg.ShowDialog() == DialogResult.Cancel)
                {
                    cards.Clear();
                }
            }

        }

        /// <summary>
        /// 添加选中的卡类型
        /// </summary>
        /// <param name="type"></param>
        private void ManualAddCard(CARD_TYPE type,int cardID,int startAxisID,int totalAxisNum,int diNum,int doNum)
        {
            ICard card = new Adlink();
            if (type == CARD_TYPE.ADLINK_2048)
            {
                card = new Adlink();
                card.CardType = "PCI_20408C";
            }
            else if (type == CARD_TYPE.ADLINK_833X)
            {
                card = new Adlink833X();
                card.CardType = "Adlink_833X";
            }

            else if (type == CARD_TYPE.ACS)
            {
                card = new ACSController();
                card.CardType = "ACSController";
            }
            else if (type == CARD_TYPE.M60)
            {
                card = new LCT_M60();
            }
            else if (type == CARD_TYPE.Beckoff)
            {
                card = new Beckhoff();
                card.CardType = "Beckhoff";
            }
            else if (type == CARD_TYPE.ADLINK_IO_CARD)
            {
                card = new AdlinkIOCard();
                card.CardType = "ADLINK_IO_CARD";
            }
            else if (type == CARD_TYPE.UWC348A_IO_CARD)
            {
                card = new UWC348AIOCard();
                card.CardType = "UWC348A_IO_CARD";
            }


            //Test start
            //AdlinkMotionCards.Clear();
            //for (int i = 0; i < 2; i++)//模拟找到两张卡，每个卡4个轴
            //{
            //    card = new Adlink();
            //    card.TotalAxis = 4;
            //    card.StartAxisID = i * card.TotalAxis;
            //    card.CardID = i;
            //    card.CardType = "PCI_20408C";
            //    card.InputCount = 24;
            //    card.OutputCount = 24;
            //    MotionCards.Add(card);
            //}
            //Test end
            List<ICard> cards = new List<ICard>();
            if (card is IMotionCard)
            {
                try
                {
                    IMotionCard motionCard = (IMotionCard)card;
                    motionCard.TotalAxis = totalAxisNum;
                    motionCard.StartAxisID = startAxisID;
                    motionCard.CardID = cardID;
                    motionCard.InputCount = diNum;
                    motionCard.OutputCount = doNum;
                    //card.CardInit(ref cards, 0);
                    MotionCards.Add(motionCard);
                }
                catch (Exception ex)
                {
                    traceLogger.Error(ex.StackTrace);
                }

                string content = $"Manual Add  Motion Card （{card.CardType}）" + Environment.NewLine;
                foreach (IMotionCard c in MotionCards)
                {
                    content += "Card ID:" + c.CardID + "  Includs " + c.TotalAxis + " Axes" + Environment.NewLine;
                }
                Dlg_Confirm comfirmDlg = new Dlg_Confirm("Confirm", content + "Click Confirm Add to Resources Tree");
                comfirmDlg.Size = new Size(400, 400);
                if (comfirmDlg.ShowDialog() == DialogResult.Cancel)
                {
                    MotionCards.Clear();
                }
            }
            else
            {
                //for (int i = 0; i < 4; i++)//模拟找到两张卡，每个卡32个DI/DO
                //{
                //    card = new AdlinkIOCard();
                //    card.CardID = i;
                //    card.CardType = "PCI_7442";
                //    card.InputCount = 32;
                //    card.OutputCount = 32;
                //    cards.Add(card);
                //}
                card.CardID = cardID;
                card.InputCount = diNum;
                card.OutputCount = doNum;
                cards.Add(card);
                IOCards.AddRange(cards);

                string content = $"Manual Add  IO Card ({card.CardType})" + Environment.NewLine;
                Dlg_Confirm comfirmDlg = new Dlg_Confirm("Confirm", content + "Click Confirm Add to Resources Tree");
                comfirmDlg.Size = new Size(400, 400);
                if (comfirmDlg.ShowDialog() == DialogResult.Cancel)
                {
                    cards.Clear();
                }
            }

        }

    }
}
